/*! ******************************************************************************
 *
 * Created on 2021年9月25日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.pentaho.di.ui.util;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @ClassName: IoUtils
 * @Description: TODO
 * @author yzbdl@yzbdl.ac.cn
 */
public class IoUtils {

	public static void closeQuietly(InputStream input) {
		closeQuietly((Closeable) input);
	}

	public static void closeQuietly(OutputStream output) {
		closeQuietly((Closeable) output);
	}

	public static void closeQuietly(Closeable closeable) {
		try {
			if (closeable != null) {
				closeable.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void closeQuietly(Closeable... closeables) {
		for (Closeable closeable : closeables) {
			closeQuietly(closeable);
		}
	}

	public static long copy(InputStream in, OutputStream out, int bufferSize) throws IOException {
		byte[] buff = new byte[bufferSize];
		return copy(in, out, buff);
	}

	public static long copy(InputStream in, OutputStream out) throws IOException {
		byte[] buff = new byte[1024];
		return copy(in, out, buff);
	}

	public static long copy(InputStream in, OutputStream out, byte[] buff) throws IOException {
		long count = 0;
		int len = -1;
		while ((len = in.read(buff, 0, buff.length)) != -1) {
			out.write(buff, 0, len);
			count += len;
		}
		return count;
	}
}
